package de.axone.webtemplate.element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.axone.tools.Str;
import de.axone.webtemplate.Decorator;
import de.axone.webtemplate.Renderer;
import de.axone.webtemplate.WebTemplateException;
import de.axone.webtemplate.form.Translator;

public class AbstractCombinedHtmlInputElement implements Renderer, HtmlInput {
	
	private ArrayList<HtmlInput> subElements = new ArrayList<HtmlInput>();

	@Override
	public void render( Object object, HttpServletRequest request,
			HttpServletResponse response, Translator translator )
			throws IOException, WebTemplateException, Exception {
		
	}
	
	protected String combine( List<String> singleValues ){
		
		return Str.join( ";", singleValues );
	}
	protected List<String> split( String combinedValue ){
		
		return Arrays.asList( combinedValue.split( ";" ) );
	}

	private String name;
	@Override
	public void setName( String name ) {
		this.name = name;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setValue( String value ) {
		
		List<String> singleValues = split( value );
		
		for( int i = 0; i < subElements.size(); i++ ){
			subElements.get( i ).setValue( singleValues.get( i ) );
		}
	}

	@Override
	public String getValue() {
		
		LinkedList<String> values = new LinkedList<String>();
		
		for( HtmlInput input : subElements ){
			values.addLast( input.getValue() );
		}
		
		return combine( values );
	}

	@Override
	public void initialize( HttpServletRequest request ) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setDecorator( Decorator renderer ) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void renderElement( Object object, HttpServletRequest request,
			HttpServletResponse response, Translator translator )
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	private boolean valid;
	@Override
	public void setValid( boolean valid ) {
		
		this.valid = valid;
	}
	@Override
	public boolean isValid() {
		return valid;
	}


	private List<String> messages;
	@Override
	public void setMessages( List<String> messages ) {
		this.messages = messages;
		
	}
	@Override
	public List<String> getMessages() {
		return messages;
	}

}
